<template>
  <a-modal
    :title="title"
    :width="630"
    :visible="visible"
    centered
    :maskClosable="false"
    @ok="handleOk"
    :okText="$t('保存')"
    @cancel="handleCancel"
    :cancelText="$t('关闭')"
    v-drag
  >
    <template slot="footer">
      <div style="text-align: center">
        <a-button type="primary" class="ant-btn" @click="handleCancel">{{ $t('关闭') }}</a-button>
        <a-button type="primary" class="ant-btn ant-btn-primary" @click="handleOk">{{ $t('保存') }}</a-button>
        <a-button type="primary" class="ant-btn ant-btn-primary" @click="testPrint">{{ $t('打印测试') }}</a-button>
      </div>
    </template>
    <a-row type="flex" justify="start" align="middle" style="background-color: #fff7ea; height: 40px">
      <a-col :span="20" style="margin-left: 25px">
        <label class="txt_body" style="color: #e28909">{{
          $t('说明：请先选择对应的打印机，打印测试成功后方可正常使用。')
        }}</label></a-col
      >
    </a-row>
    <div>
      <div class="cus-ant-form">
        <a-space direction="vertical">
          <div style="margin-left: 84px; margin-top: 40px">
            <label>{{ $t('选择打印机') }}：</label>
            <a-select
              style="width: 320px"
              :placeholder="$t('请选择打印机')"
              v-model="model.selectedPrinter"
              class="cus-select-style1"
            >
              <a-select-option v-for="item in printList" :key="item" :value="item">{{ item }}</a-select-option>
            </a-select>
          </div>
          <div style="margin-left: 100px; margin-bottom: 0">
            <label>{{ $t('设置字号') }}：</label>
            <a-select
              style="width: 320px"
              :placeholder="$t('请选择字号')"
              v-model="model.selectedFontSize"
              class="cus-select-style1"
            >
              <a-select-option v-for="item in fontSizeList" :key="item" :value="item">{{ item }}</a-select-option>
            </a-select>
          </div>
          <div style="margin-left: 84px; margin-bottom: 40px">
            <a-checkbox v-model="model.printLogo" :disabled="picture == ''" @change="changeState">{{
              $t('打印商家Logo')
            }}</a-checkbox>
          </div>
        </a-space>
      </div>
      <div style="height: 30px">
        <label style="color: red; display: inline-block; margin-left: 30px" v-text="notifyInfo"></label>
      </div>
    </div>
  </a-modal>
</template>
<script>
import { isEmpty, getAction } from '@/api/manage'
export default {
  name: 'printSetting',
  data() {
    return {
      title: this.$t('打印机设置'),
      visible: false,
      notifyInfo: '',
      model: {
        selectedPrinter: '',
        selectedFontSize: '9',
        printLogo: false,
      },
      printList: [],
      fontSizeList: ['3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'],
      picture: '',
    }
  },
  mounted() {},
  methods: {
    changeState(e) {
      plugin.setData('C:\\ProgramData\\leyudm\\Manage\\data.json', 'printLogo', e?1:0)
    },
    keydown() {
      let that = this
      if (event != null) {
        if (event.keyCode == 13) {
          var element = event.srcElement || event.target
          if (document.all) {
            event.keyCode = 9
          } else {
            event.preventDefault()
            that.handleOk()
          }
          event.keyCode = 0
          return
        }
      }
    },
    show() {
      let that = this
      that.visible = true
      getAction('/shop/bisShop/list').then((res) => {
        if (res.success) {
          if (res.result.records[0].shopPicture) {
            that.picture = res.result.records[0].shopPicture
          }
        }
      })
      that.init()
    },
    close() {
      this.visible = false
    },
    handleOk() {
      let that = this
      if (isEmpty(that.model.selectedPrinter)) {
        that.errorMessage(that.$t('请选择打印机'))
        return
      }
      plugin.setData('C:\\ProgramData\\leyudm\\Manage\\data.json', 'printName', that.model.selectedPrinter)
      plugin.setData('C:\\ProgramData\\leyudm\\Manage\\data.json', 'printFontSize', that.model.selectedFontSize)
      plugin.setData('C:\\ProgramData\\leyudm\\Manage\\data.json', 'printLogo', that.model.printLogo?1:0)
      that.$message.success(that.$t('保存成功 '))
    },
    handleCancel() {
      let that = this
      that.close()
    },
    init() {
      let that = this
      plugin.getPrints().then((result) => {
        that.printList = result
      })
      that.model.selectedPrinter = plugin.getData(
        'C:\\ProgramData\\leyudm\\Manage\\data.json',
        'printName',
        localStorage.getItem('printName')
      )
      that.model.selectedFontSize = plugin.getData(
        'C:\\ProgramData\\leyudm\\Manage\\data.json',
        'printFontSize',
        localStorage.getItem('printFontSize')
      )
      let printLogo=plugin.getData(
        'C:\\ProgramData\\leyudm\\Manage\\data.json',
        'printLogo',
        localStorage.getItem('printLogo')
      ) 
      if (isEmpty(printLogo)) {
        plugin.setData('C:\\ProgramData\\leyudm\\Manage\\data.json', 'printLogo', 0)
        printLogo = Number(plugin.getData(
          'C:\\ProgramData\\leyudm\\Manage\\data.json',
          'printLogo',
          localStorage.getItem('printLogo')
        ))
      }
      that.model.printLogo=printLogo==0?false:true
      if (isEmpty(that.model.selectedFontSize)) {
        plugin.setData('C:\\ProgramData\\leyudm\\Manage\\data.json', 'printFontSize', '9')
        that.model.selectedFontSize = plugin.getData(
          'C:\\ProgramData\\leyudm\\Manage\\data.json',
          'printFontSize',
          localStorage.getItem('printFontSize')
        )
      }
    },
    errorMessage(message) {
      let that = this
      that.notifyInfo = message
    },
    testPrint() {
      let that = this
      let result = plugin.getData(
        'C:\\ProgramData\\leyudm\\Manage\\data.json',
        'printName',
        localStorage.getItem('printName')
      )
      if (isEmpty(result)) {
        that.errorMessage(that.$t('请先设置打印机'))
        return
      }
      let fontSize = plugin.getData('C:\\ProgramData\\leyudm\\Manage\\data.json', 'printFontSize', 9)
      let printElementList = []
      if (typeof getPrintList != 'undefined') {
        printElementList.push({
          text: that.$t('   商品名称'),
          fontSize: 6,
          weight: 700,
          fontName: '宋体',
          left: 0,
          top: 10,
          barCode: { code: '1234567890', left: 0, top: 25, width: 140, height: 50 },
        })
        printElementList.push({
          text: that.$t('       5000分'),
          fontSize: fontSize,
          weight: 400,
          fontName: '宋体',
          left: 0,
          top: 150,
        })
        let pname = plugin.getData(
          'C:\\ProgramData\\leyudm\\Manage\\data.json',
          'printName',
          localStorage.getItem('printName')
        )
        plugin.print(pname, printElementList)
      } else {
        printElementList.push({
          text: that.$t('    商品名称'),
          fontSize: 12,
          weight: 700,
          fontName: '宋体',
          left: 60,
          top: 16,
          /* pageSize: {
            width: 360,
            height: 110,
          }, */
        })
        printElementList.push({
          text: that.$t(''),
          barCode: { code: '1234567890', left: 60, top: 40, width: 150, height: 35 },
        })
        printElementList.push({
          text: that.$t('         5000分'),
          fontSize: fontSize,
          weight: 400,
          fontName: '宋体',
          left: 60,
          top: 80,
        })

        let pname = plugin.getData(
          'C:\\ProgramData\\leyudm\\Manage\\data.json',
          'printName',
          localStorage.getItem('printName')
        )
        plugin.print(pname, printElementList)
      }
    },
  },
}
</script>

<style lang="less">
.cus-input-password.ant-input-password .ant-input {
  height: 50px;
  font-size: 18px;
}
</style>
